package com.zking.dao;

import com.zking.entity.PageManager;
import com.zking.entity.Student;
import com.zking.entity.Teacher;
import com.zking.utils.DBPoolUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

public class StudentDao extends BaseDao{

    private static QueryRunner runner = new QueryRunner(DBPoolUtils.getDataSource());


    /**
     * 获取总数据数
     */
    public int getStudentTotal (){
        String sql = "select count(*) from sims_student";
        return queryScalarForInteger(sql);
    }

    /**
     * 查询
     * @param pm
     * @return
     */
    public List<Student> queryStudent(PageManager pm){
        String sql = "select * from sims_student limit ?,?";
        Object[] params = {pm.getStart() , pm.getRn()};
        return queryObjects(sql, Student.class,params);
    }

    /**
     * 添加新学生用户
     * @param s
     * @return
     */
    public int addStu(Student s){
        //用户创建时，自己添加姓名，昵称，身份证号，手机号，邮箱
        String sql = "insert into sims_student(STUDENT_NAME,NICK_NAME,ID_CARD_NO,MOBILE_PHONE,EMAIL)"
                +"values(?,?,?,?,?)";
        Object[] params ={s.getSTUDENT_NAME(),s.getNICK_NAME(),s.getID_CARD_NO(),s.getMOBILE_PHONE(),s.getEMAIL()};
        try {
            return runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除老师用户
     * @param id
     * @return
     */
    public int delStu(int id){
        String sql = "delete from sims_student where id = ?";
        try {
            return runner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询一个用户的数据
     * @param id
     * @return
     */
    public Student queryStu(int id){
        String sql = "select * from sims_student where id = ?";
        try {
            return runner.query(sql,new BeanHandler<>(Student.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用户自己更改除了消费金额和积分以外的信息
     * @param t
     * @return
     */
    public int updateTea(Student t){
        String sql = "update sims_student set STUDENT_NAME=?, NICK_NAME=?, ID_CARD_NO=?, MOBILE_PHONE=?, GENDER=?, " +
                "MONTHLY_SALARY=?, BIRTH=?, AVATAR=?, HEIGHT=?, WEIGHT=?, NATION=?," +
                "POLITICAL=?, MARITAL=?, DOMICILE_PLACE_PROVINCE=?, DOMICILE_PLACE_CITY=?, DOMICILE_PLACE_ADDRESS=?, HOBBY=?, INTRO=?, PRESENT_ADDRESS=?," +
                "EMAIL=?, ENTRY_DATE=?, INTRO=? where STUDENT_ID =?";
        Object[] params = {t.getSTUDENT_NAME(),t.getNICK_NAME(),t.getID_CARD_NO(),t.getMOBILE_PHONE(),t.getGENDER(),t.getMONTHLY_SALARY(),t.getBIRTH(),t.getAVATAR(),
                t.getHEIGHT(),t.getWEIGHT(),t.getNATION(),t.getPOLITICAL(),t.getMARITAL(),t.getDOMICILE_PLACE_PROVINCE(),t.getDOMICILE_PLACE_CITY(),
                t.getDOMICILE_PLACE_ADDRESS(),t.getHOBBY(),t.getINTRO(),t.getPRESENT_ADDRESS(),t.getEMAIL(),t.getENTRY_DATE(),t.getINTRO(),t.getSTUDENT_ID()};
        try {
            return runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
